<template>
  <el-form :model="nodeModalForm" ref="nodeModal" label-placement="left" label-width="120px" :rules="nodeModalRules"
    size="small">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="节点名称" prop="nodeName">
          <el-input v-model.trim="nodeModalForm.nodeName"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="主键列" prop="groupName">
          <el-select v-model="nodeModalForm.groupName" class="w_100" clearable filterable>
            <el-option v-for="item in fieldsList" :key="item.alias" :label="item.alias" :value="item.alias">
              <span style="float: left">{{ item.alias }}</span>
              <span style="float: right;color: var(--el-text-color-secondary);font-size: 13px;">{{
                  item.DATA_TYPE
                }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="转换列名" prop="nameColumn">
          <el-input v-model.trim="nodeModalForm.nameColumn" placeholder="列名称"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="填充数据列名" prop="dataColumn">
          <el-input v-model.trim="nodeModalForm.dataColumn" placeholder="行转列的列"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <table border="1" align="center" class="add_table w_100" cellspacing="0">
          <tr>
            <th width="70"><span class="iconfont icon-Add cursor_point" @click="handleAddRTC"></span></th>
            <th>转换列</th>
            <th>转换名称</th>
          </tr>
          <tr v-for="(item,index) in nodeModalForm.dataRtc" :key="index">
            <td width="70" align="center">
              <span class="iconfont icon-Add cursor_point px_2" @click="handleAddRTC(index)"></span>
              <span class="iconfont icon-close cursor_point px_2" @click="handleDeleteRTC(index)"></span>
            </td>
            <td>
              <el-select v-model="item.field" clearable filterable placeholder="字段" class="w_100"
                @change="sourceFieldChange(index)">
                <el-option v-for="item in sourceFieldList" :key="item.name" :label="item.alias" :value="item.alias">
                  <span style="float: left">{{ item.alias }}</span>
                  <span style="float: right;
                      color: var(--el-text-color-secondary);
                      font-size: 13px;">{{ item.DATA_TYPE }}</span>
                </el-option>
              </el-select>
            </td>
            <td>
              <el-input v-model.trim="item.alias" clearable></el-input>
            </td>
            <td></td>
          </tr>
        </table>
      </el-col>
    </el-row>
    <div class="nodeModal-footer">
      <span class="dialog-footer">
        <el-button plain @click="handleConfigCancel">取消</el-button>
        <el-button type="primary" @click="handleConfigConfirm">更新</el-button>
      </span>
    </div>
  </el-form>
</template>

<script>
import { computed, defineComponent, inject, onMounted, ref } from "vue";
import { useEtlStore } from "../../../../../store/flowEtl";
import { numberType } from "../../../../../utils/constant";

export default defineComponent({
  name: "node-config-rtc",
  props: {
    node: Object,
  },
  components: {},
  setup(props, ctx) {
    const etlStore = useEtlStore();
    const nodeList = computed(() => etlStore.nodeList);
    const nodeConfigClose = inject("nodeConfigClose");
    const nodeModalForm = ref({
      nodeName: "",
      groupName: "",
      dataColumn: "",
      nameColumn: "",
      dataRtc: [],
      fields: [],
    });
    const fieldsList = ref([]);
    const sourceFieldList = ref([]);
    const nodeModal = ref();
    const tableLoading = ref(false);
    const tableList = ref([]);
    const nodeModalRules = ref({
      nodeName: {
        required: true,
        message: "请输入节点名称",
        trigger: "blur",
      },
      groupName: {
        required: true,
        message: "请选择分组列",
        trigger: "change",
      },
      dataColumn: {
        required: true,
        message: "请输入填充数据列",
        trigger: "change",
      },
      nameColumn: {
        required: true,
        message: "请输入转换后列名",
        trigger: "change",
      },
    });
    const handleConfigCancel = () => {
      nodeConfigClose();
    };
    const nodeConfigUpdate = (node) => {
      let fields = new Set([node.groupName, node.nameColumn, node.dataColumn]);
      if (fields.size !== 3) {
        ElMessage.error("存在相同的别名，请修改！！！");
        return;
      }
      node.fields = [];
      let groupColumn = fieldsList.value.find(
        (item) => item.alias === node.groupName
      );
      node.fields = [
        groupColumn,
        {
          COLUMN_COMMENT: node.nameColumn,
          COLUMN_NAME: node.nameColumn,
          DATA_TYPE: "varchar",
          alias: node.nameColumn,
        },
        {
          COLUMN_COMMENT: node.dataColumn,
          COLUMN_NAME: node.dataColumn,
          DATA_TYPE: "numeric",
          alias: node.dataColumn,
        },
      ];
      let nodes = [...nodeList.value].map((item) => {
        if (item.id === node.id) {
          item.status = true;
          item.nodeName = node.nodeName;
          item.config.groupName = node.groupName;
          item.config.dataColumn = node.dataColumn;
          item.config.nameColumn = node.nameColumn;
          item.config.dataRtc = node.dataRtc;
          item.config.fields = node.fields;
        }
        return item;
      });
      etlStore.setNodeList(nodes);
      nodeConfigClose();
    };
    const handleConfigConfirm = () => {
      nodeModal.value.validate((valid) => {
        if (valid) {
          nodeConfigUpdate(nodeModalForm.value);
        } else {
          return false;
        }
      });
    };
    const resetForm = () => {
      fieldsList.value = [];
      nodeModalForm.value = {
        id: null,
        nodeName: "",
        groupName: "",
        dataColumn: "",
        nameColumn: "",
        dataRtc: [],
        fields: [],
      };
    };
    const handleAddRTC = (index) => {
      if (index) {
        nodeModalForm.value.dataRtc = [
          ...nodeModalForm.value.dataRtc.splice(0, index + 1),
          {
            field: "",
            alias: "",
          },
          ...nodeModalForm.value.dataRtc.splice(
            index - 1,
            nodeModalForm.value.dataRtc.length
          ),
        ];
      } else {
        nodeModalForm.value.dataRtc.push({
          alias: "",
          field: "",
        });
      }
    };
    const handleDeleteRTC = (index) => {
      nodeModalForm.value.dataRtc.splice(index, 1);
    };
    const sourceFieldChange = (index) => {
      nodeModalForm.value.dataRtc[index].alias =
        nodeModalForm.value.dataRtc[index].field;
    };
    onMounted(() => {
      resetForm();
      fieldsList.value = props.node.originFields;
      nodeModalForm.value = {
        id: props.node.id,
        nodeName: props.node.nodeName || "",
        groupName: props.node.config.groupName || "",
        dataColumn: props.node.config.dataColumn || "",
        nameColumn: props.node.config.nameColumn || "",
        dataRtc: props.node.config.dataRtc || [],
        fields: props.node.config.fields || [],
      };
      sourceFieldList.value = fieldsList.value.filter((item) => {
        let date_type = item.DATA_TYPE;
        let regType = date_type.replace(/[0-9]+/g, "");
        if (numberType.includes(regType)) {
          return item;
        }
      });
    });
    return {
      nodeList,
      fieldsList,
      tableLoading,
      nodeModalForm,
      tableList,
      nodeModalRules,
      nodeModal,
      nodeConfigClose,
      sourceFieldList,
      handleAddRTC,
      handleDeleteRTC,
      resetForm,
      handleConfigCancel,
      handleConfigConfirm,
      nodeConfigUpdate,
      sourceFieldChange,
    };
  },
});
</script>

<style scoped>
.field-table {
  padding: 0 12px 24px;
}

.nodeModal-footer {
  padding: var(--el-dialog-padding-primary);
  padding-top: 10px;
  text-align: right;
  box-sizing: border-box;
}
</style>